//
//  UserExchangeDiamondController.m
//  CheekiOS
//
//  Created by mac on 2017/6/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserExchangeDiamondController.h"
#import "UserExchangeDiamondCell.h"
@interface UserExchangeDiamondController ()<UITableViewDelegate,UITableViewDataSource,UserExchangeDiamondCellDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *NumberArray;
@property (nonatomic, strong)NSArray *MoneyArray;

@property (nonatomic, strong)UserpersionlInfoModel *infoModel;



@end

@implementation UserExchangeDiamondController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationBarHiddenNo];
    [self selfTitleFontColor];
    self.title = @"兑换钻石";
    
    _NumberArray = @[@"65",@"195",@"325",@"650"];
    _MoneyArray = @[@"100",@"300",@"500",@"1000"];
    
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    
    [self requestUserInfo];
}



- (void)requestUserInfo{
    
    [HcardHttpmanager getPersionalInfosuccess:^(id sucess) {
        
        self.infoModel = [UserpersionlInfoModel mj_objectWithKeyValues:sucess[@"res"]];
        

        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return 4;

    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *userAccountCellIdentOne = @"userAccountCellIdentOne";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:userAccountCellIdentOne];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userAccountCellIdentOne];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        
        UILabel *numberLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, KScreenWidth, 40)];
        numberLab.text = [NSString stringWithFormat:@"%.2f",[_infoModel.charm1 floatValue]];
        numberLab.textColor = ColorI(0x5978b6);
        numberLab.textAlignment = NSTextAlignmentCenter;
        numberLab.font = FontP(25);
        [cell.contentView addSubview:numberLab];
        
        UILabel *textLab = [[UILabel alloc]initWithFrame:CGRectMake(0, numberLab.bottom+5, KScreenWidth, 30)];
        textLab.text = @"魅力值余额";
        textLab.textColor = [UIColor blackColor];
        textLab.textAlignment = NSTextAlignmentCenter;
        textLab.font = FontP(16);
        [cell.contentView addSubview:textLab];
        return cell;
    }else{
        
        UserExchangeDiamondCell *cell = [UserExchangeDiamondCell cellWithTableView:tableView indexPath:indexPath];
     
        cell.numberArray = _NumberArray;
        cell.numberMoneyArray = _MoneyArray;
        cell.delegate = self;
        
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }else{
        return 60;
    }
}


-(void)UserExchangenumBtnClickAction:(UIButton *)sender{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确认兑换吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *typeNote = @"魅力值换购";
        NSString *strNumber= _NumberArray[sender.tag];
        NSString *strCharm1 = [NSString stringWithFormat:@"%.2f",[_MoneyArray[sender.tag] floatValue]];
        
        
        
        [HcardHttpmanager inOutCharmWithCharm:strNumber withCost:@"" withNote:typeNote withType:KInCharmType withTransNo:@"" withCharm1:strCharm1 withfirstCharge:@""  success:^(id result) {
            
            if ([result[@"code"] isEqualToString:@"ok"]) {
                [SVProgressHUD showWithStatus:@"兑换成功"];
                [self requestUserInfo];
                
            }
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                
            });
            
            
            
        } failure:^(NSError *error) {
            
        }];

    }];
    UIAlertAction *CAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:confirAction];
    [alertVC addAction:CAction];

    [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    
    
}

@end
